﻿using System;
using System.Text;
using System.Windows;
using System.IO;
using System.Diagnostics;
using System.Data;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace GetCDSData
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly string className = "T_MEAN_DAY_M,T_MAX_DAY_M,T_MIN_DAY_M,R_DAY_M,"+
            "D0_MEAN_DAY_M,D0_MAX_DAY_M,D0_MIN_DAY_M,S_DAY_M,";
        private readonly string dbFile = @"G:\U盘\程序开发\1981-2010年30年气候整编资料\CDS_51639.DAT";
        private DataSet ds;
        public MainWindow()
        {
            InitializeComponent();
        }
        private void ElementNameComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            string tableName = ElementNameComboBox.SelectedItem.ToString();
            DataView dv = new DataView(ds.Tables[tableName]);
            ShowDataGrid.ItemsSource = dv;
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            GetCDSFileData(out ds);
            for (int i = 0; i < ds.Tables.Count; i++)
            {
                ElementNameComboBox.Items.Add(ds.Tables[i].TableName);
            }
            ElementNameComboBox.SelectedIndex = 1;
            //创建DataView的实例dv，并指定其默认table
            DataView dv = new DataView(ds.Tables[1]); 
            ShowDataGrid.ItemsSource = dv;
        }
        private void GetCDSFileData(out DataSet ds)
        {
            FileStream fs;
            ds = new DataSet();
            try
            {
                fs = new FileStream(dbFile, FileMode.Open);
            }
            catch { return; }
            if (fs.Length != 4832256) return;
            byte[] bytes = new byte[384];
            while (true)
            {
                byte[] classByte = new byte[20];
                byte[] dateByte = new byte[2];
                byte[] dataByte = new byte[354];

                int r = fs.Read(bytes, 0, bytes.Length);
                if (r == 0) break;
                Array.Copy(bytes, 0, classByte, 0, 20);
                Array.Copy(bytes, 24, dateByte, 0, 2);
                Array.Copy(bytes, 30, dataByte, 0, 350);
                
                string tableName = (Encoding.UTF8.GetString(classByte)).Trim();
                string[] strTemp = tableName.Split('_');
                strTemp[strTemp.Length - 1] = Regex.Replace(strTemp[strTemp.Length - 1], @"\d", "");
                if (className.IndexOf(string.Join("_",strTemp) + ",") == -1) continue;
                int intYear = ((Int16)((dateByte[1] << 8) | dateByte[0]));
                if (intYear > 2010) continue;
                string rowName = intYear + "";
                try
                {
                    if (ds.Tables[tableName] == null)
                    {
                        DataTable dt = new DataTable();
                        dt.TableName = tableName;
                        ds.Tables.Add(dt);
                        //添加日期列，共计31列
                        for (int i = 0; i <= 31; i++)
                        {
                            DataColumn dc = new DataColumn();
                            if (i == 0) dc.ColumnName = "yyyy";
                            else dc.ColumnName = i + "";
                            ds.Tables[tableName].Columns.Add(dc);
                        }
                    }
                    DataRow dr = ds.Tables[tableName].NewRow();
                    for (int i = 0; i < ds.Tables[tableName].Columns.Count; i++)
                    {
                        if (i == 0)
                        {
                            dr[i] = rowName;
                            continue;
                        }
                        Int16 intTemp = ((Int16)((dataByte[4 * (i - 1) + 3] << 8) | dataByte[4 * (i - 1) + 2]));
                        if (intTemp == 0)
                        {
                            //降水微量=0，无降水=/,其他无数据均为/
                            if (string.Join("_", strTemp) == "R_DAY_M")
                            {
                                if (dataByte[4 * (i - 1)] == 0x04) dr[i] = "0";
                                else dr[i] = "/";
                            }
                            else
                            {
                                if (dataByte[4 * (i - 1)] == 0x01) dr[i] = "/";
                                else dr[i] = intTemp + "";
                            }
                        }
                        else dr[i] = intTemp + "";
                    }
                    ds.Tables[tableName].Rows.Add(dr);
                }
                catch { }
            }
            fs.Close();
        }

    }
}
